package kyssion.leetcode.num51_100;

public class code63_不同路径2 {

    public static void main(String[] args) {
        System.out.println(new code63_不同路径2().uniquePathsWithObstacles(
                new int[][]{
                        {0}, {1}
                }
        ));
    }

    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        if (obstacleGrid.length == 0 ||
                obstacleGrid[0].length == 0 || obstacleGrid[0][0] == 1) {
            return 0;
        }
        int[] dp = new int[obstacleGrid[0].length];
        dp[0] = 1;
        for (int a = 0; a < obstacleGrid.length; a++) {
            for (int b = 0; b < obstacleGrid[a].length; b++) {
                if (obstacleGrid[a][b] == 1) {
                    dp[b] = 0;
                } else if (b > 0) {
                    dp[b] = dp[b - 1] + dp[b];
                }
            }
        }
        return dp[obstacleGrid[0].length - 1];
    }
}
